// com/zzyl/service/impl/NursingPlanServiceImpl.java
package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<NursingPlan> getAllNursingPlans() {
        return nursingPlanMapper.selectAll();
    }

    /**
     * 添加
     * @param dto
     */
    @Override
    public void addNursingPlan(NursingPlanDto dto) {
        NursingPlan bean = BeanUtil.toBean(dto, NursingPlan.class);
        //设置父内容状态
        bean.setStatus((byte) 0);
        nursingPlanMapper.insert(bean);
        //子内容
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        for (NursingProjectPlan p : projectPlans) {
            p.setPlanId(bean.getId());
        }
        nursingProjectPlanMapper.addList(projectPlans);

    }

    /**
     * 分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        //正常查询数据
        Page<NursingPlan> list = (Page<NursingPlan>) nursingPlanMapper.selectByPage(name, status);
        // 将 PO 转换为 VO
        PageResponse<NursingPlanVo> result = PageResponse.of(list, NursingPlanVo.class);
        return result;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        NursingPlan plan = nursingPlanMapper.selectById(id);
       List<NursingProjectPlan> projectPlan = nursingProjectPlanMapper.getByPlanId(id);
        NursingPlanVo result = BeanUtil.toBean(plan, NursingPlanVo.class);
        result.setProjectPlans(projectPlan);
        return result;
    }

    @Override
    public void updateNursingPlan(NursingPlanDto dto) {
        NursingPlan bean = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlanMapper.update(bean);

        //根据删除所有子表内容
        nursingProjectPlanMapper.deleteByPlanId(dto.getId());
        //在添加子表内容
        for (NursingProjectPlan p : dto.getProjectPlans()) {
            p.setPlanId(bean.getId());
        }
        nursingProjectPlanMapper.addList(dto.getProjectPlans());

    }

    /**
     * 根据id删除计划
     * @param id
     */

    @Transactional
    public void deleteNursingPlanById(Long id) {
        nursingProjectPlanMapper.deleteByPlanId(id);
        nursingPlanMapper.deleteById(id);
    }

    @Override
    public void enableOrDisable(Long id, Byte status) {
        nursingPlanMapper.updateStatus(id, status);
    }


}